package com.lin.learning.questionmanagement.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czy.learning.questionnature.mapper.QuestionNatureMapper;
import com.czy.learning.questionnature.service.QuestionNatureService;
import com.lin.learning.questionmanagement.dto.AddQuestionDto;
import com.lin.learning.questionmanagement.dto.GetQuestionsDto;
import com.lin.learning.questionmanagement.dto.UpdateQuestionDto;
import com.lin.learning.questionmanagement.mapper.QuestionMapper;
import com.lin.learning.questionmanagement.service.QuestionService;
import com.lin.learning.questionmanagement.utils.BeanCopyUtils;
import com.lin.learning.questionmanagement.vo.PageVo;
import com.our.learning.infrastructure.po.Question;
import com.our.learning.infrastructure.po.QuestionNature;
import com.our.learning.infrastructure.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper,Question> implements QuestionService  {


    @Autowired
    private  QuestionMapper questionMapper;

    @Autowired
    private QuestionNatureMapper questionNatureMapper;


    @Override
    public Question getQuestionByIdLocal(Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(id),Question::getId,id);
        Question question=getOne(lambdaQueryWrapper);
        return question;
    }

    @Override
    public List getQuestionByIdsLocal(List id) {
        List<Question> questions = questionMapper.selectBatchIds(id);
        return questions;
    }

    @Override
    public List<Question> getQuestionByTypeAndDifficultyLocal(String type, Integer difficulty) {
        return questionMapper.getOneByTypeAndDifficulty(type, difficulty);
    }


    @Override
    public ResponseResult getQuestionsByType(Integer page, Integer perPage,String type) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getType,type);
        Page<Question> pages=new Page<>(page,perPage);
        page(pages,lambdaQueryWrapper);
        List<Question> list=pages.getRecords();
        PageVo pageVo=new PageVo(list,pages.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getQuestions(GetQuestionsDto getQuestionsDto) {
        List<Integer> idlist=getQuestionsDto.getIds();
        List<Question> questions = questionMapper.selectBatchIds(idlist);
        // TODO: 2023-06-21 可能还需要加上其他字段,性质还没适配
/*        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getType,type);
        List<Question> questions1 = questionMapper.selectList(lambdaQueryWrapper);*/
        return ResponseResult.okResult(questions);
    }

    @Override
    public ResponseResult questionList(Integer page, Integer perPage,Integer id,String type) {
        if(id!=null){
            List<Question> list=new ArrayList<>();
            Question question=getMyQuestionByIdLocal(id);
            if (question==null){
                PageVo pageVo=new PageVo(list,0L);
                return ResponseResult.okResult(pageVo);
            }else{
                list.add(question);
                PageVo pageVo=new PageVo(list,1L);
                return ResponseResult.okResult(pageVo);
            }
        } else if (type != null&&!"".equals(type)) {
            return getQuestionsByType(page,perPage,type);
        }
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Question::getId);
        Page<Question> pages=new Page<>(page,perPage);
        page(pages,lambdaQueryWrapper);
        List<Question> list=pages.getRecords();
        PageVo pageVo=new PageVo(list,pages.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addQuestion(AddQuestionDto addQuestionDto) {
        Question question= BeanCopyUtils.copyBean(addQuestionDto,Question.class);
        save(question);
        QuestionNature questionNature=new QuestionNature(question.getId(),null,null,null,null,null,null);
        questionNatureMapper.insert(questionNature);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteQuestion(Integer id) {
        if (getById(id)==null){
            // TODO: 2023-06-20 提示没有这题
            return ResponseResult.errorResult(507,"此題不存在");
        }
        removeById(id);
        questionNatureMapper.deleteById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateQuestion(UpdateQuestionDto updateQuestionDto) {
        Question question=BeanCopyUtils.copyBean(updateQuestionDto,Question.class);
        updateById(question);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getQuestionById(Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(id),Question::getId,id);
        Question question=getOne(lambdaQueryWrapper);
        return ResponseResult.okResult(question);
    }

    public Question getMyQuestionByIdLocal(Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(id),Question::getId,id);
        Question question=getOne(lambdaQueryWrapper);
        return question;
    }

    @Override
    public String getTypeByIdLocal(Integer id){
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Question::getType).eq(Question::getId,id);
        if ("select".equals(questionMapper.selectOne(lambdaQueryWrapper).getType())){
            return "选择题";
        }else if ("blank".equals(questionMapper.selectOne(lambdaQueryWrapper).getType())){
            return "填空题";
        }else if ("picture".equals(questionMapper.selectOne(lambdaQueryWrapper).getType())){
            return "主观题（贴图）";
        }else{
            return "未知";
        }
    }

}
